package com.yjh.study.data.structure.list;

import java.util.Iterator;
import java.util.Objects;

public class ArrayList<T> extends AbstractList<T> {

    // 存数据的数组
    private Object[] innerArr = new Object[16];

    public ArrayList() {

    }

    public ArrayList(int size) {
        if (0 >= size) {
            throw new RuntimeException("数组长度必须大于0");
        }
        this.innerArr = new Object[size];
    }

    @Override
    public void add(T t) {
        addLast(t);
    }

    @Override
    public void remove(T t) {
        for (int i = 0; i < size; i++) {
            if (Objects.equals(t, innerArr[i])) {
                leftMove(i + 1, size - 1, 1);
            }
        }
        sizeIncrease(-1);
    }

    private void removeIndex(int index) {
        if (index < size) {
            leftMove(index + 1, size - 1, 1);
            sizeIncrease(-1);
        }
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            innerArr[i] = null;
        }
        size = 0;
    }

    @Override
    public Iterator<T> iterator() {
        return new Itr(0);
    }

    @Override
    public void addFirst(T t) {
        if (showGrow()) {
            grow();
        }
        rightMove(0, size - 1, 1);
        innerArr[0] = t;
        size++;
    }

    @Override
    public T getFirst() {
        return (T) innerArr[0];
    }

    @Override
    public void addLast(T t) {
        if (showGrow()) {
            grow();
        }

        innerArr[size] = t;

        sizeIncrease();
    }

    @Override
    public T getLast() {
        return (T) innerArr[size - 1];
    }

    /**
     * 批量向左移
     *
     * @param startIndex 开始下标
     * @param endIndex   结束下标
     * @param step       步长
     */
    private void leftMove(int startIndex, int endIndex, int step) {
        while (startIndex <= endIndex) {
            innerArr[startIndex - step] = innerArr[startIndex];
            startIndex++;
        }
    }

    /**
     * 批量向右移
     *
     * @param startIndex 开始下标
     * @param endIndex   结束下标
     * @param step       步长
     */
    private void rightMove(int startIndex, int endIndex, int step) {
        while (endIndex >= startIndex) {
            innerArr[endIndex + step] = innerArr[endIndex];
            endIndex--;
        }
    }

    private boolean showGrow() {
        return size >= innerArr.length;
    }

    private void grow() {
        if (Integer.MAX_VALUE == innerArr.length) {
            throw new RuntimeException("已达到最大值无法扩容");
        }

        int nextMaxContent = 2 * innerArr.length;
        if (nextMaxContent < innerArr.length) {
            nextMaxContent = Integer.MAX_VALUE;
        }

        Object[] nextInnerArr = new Object[nextMaxContent];
        System.arraycopy(innerArr, 0, nextInnerArr, 0, innerArr.length);
        innerArr = nextInnerArr;
    }

    private class Itr implements java.util.Iterator<T> {
        private int index;

        public Itr(int index) {
            this.index = index;
        }

        @Override
        public boolean hasNext() {
            return index < size;
        }

        @Override
        public T next() {
            T t = (T) innerArr[index];
            index++;
            return t;
        }

        @Override
        public void remove() {
            index--;
            removeIndex(index);
        }
    }
}
